//
//  receiveChatVoice.m
//  tcpoctest
//
//  Created by gump on 6/8/13.
//  Copyright (c) 2013 gump. All rights reserved.
//

#import "receiveChatVoice.h"

#import "XMMessageInfo.h"
#import "ChatContentInfo.h"

#import "chatuserdb.h"
#import "messagedb.h"

#import "SpeexCodec.h"
#import "sendMessageReceipt.h"

#import <AVFoundation/AVFoundation.h>

#import "chatDataSource.h"

#import "AudioPlayManager.h"
#import "MessageDataSource.h"

@implementation receiveChatVoice

-(void)processreceive
{
    //接收者 返回 16char(1),副协议char(1)区分语音、文字、图片，
    //发送者的用户名长度char(1), 发送者的用户名char(x)，二进制数据长度int,
    //二进制数据char(x),消息ID长度char(1),消息ID长度char(x),时间长度char(1),时间char(x)
    
    NSString *mail = [logicsb ReadUnencodeString];
    
    XMMessageInfo *minfo = [[XMMessageInfo alloc] init];
    minfo.strmail = mail;
    minfo.messageType = XMMT_CHAT;
    minfo.mbAlreadayArgee = false;
    minfo.mindex = [[DataControl shareControl].messageDS GetMagArrCount];
    minfo.strmsgid = [[DataControl shareControl].messageDS getMsgID];
    
    XMMessageInfo *mexits = [[MessageManager sharedManager] getMessage:minfo];
    ChatContentInfo *cinfo = [[ChatContentInfo alloc] init];
    
    if (mexits == nil)
    {
        
        NSMutableArray *chatshowarr = [[NSMutableArray alloc] init];
        [[DataControl shareControl].chatDS SetChatDicArr:chatshowarr withKey:minfo.strmail];
        [[DataBaseCtrol shareControl].chatuserdatabase insertChatDic:minfo.strmail withIsGroup:false];
        
        
        cinfo.mail = mail;
        cinfo.chatType = CT_VOICE;
        cinfo.strChatDate = getDateString();
        cinfo.iSequence = [LogicControl sharedLogicControl].CurSequence;
        cinfo.ichatReadState = CRS_UNREAD;
        
        int ilength = [logicsb ReadIntWithTT:NTT_BIG2SMALL];
                cinfo.strtext = [[FileManager shareControl] saveChatData:[logicsb ReadDataWithLength:ilength]
                                                               withIndex:[LogicControl sharedLogicControl].CurSequence];
                
                NSData *SpeexData = [NSData dataWithContentsOfFile:cinfo.strtext];
                NSData *NewPCMData = DecodeSpeexToWAVE(SpeexData);
                AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:NewPCMData error:nil];
                if (player != nil)
                {
                    cinfo.audioPlayLength = player.duration;
                    player = nil;
                }
           
        cinfo.strid = [logicsb ReadUnencodeString];
        cinfo.strChatDate = [logicsb ReadUnencodeString];
        
        [chatshowarr addObject:cinfo];
        
        minfo.badgeNum++;
        minfo.strtime = cinfo.strChatDate;
        [[MessageManager sharedManager] AddMessage:minfo];
        
        [[DataControl shareControl].chatDS addChatInfo:cinfo withID:cinfo.strid];
        
        [[DataBaseCtrol shareControl].chatuserdatabase insertChatInfo:cinfo];
        [[DataControl shareControl].UICommendDS addUICommand:UC_RECIVECHECK];
    }
    else
    {
        
        NSMutableArray *chatshowarr = [[DataControl shareControl].chatDS GetArrByKey:mail];
        if (chatshowarr != nil)
        {
            cinfo.mail = mail;
            cinfo.chatType = CT_VOICE;
            cinfo.strChatDate = getDateString();
            cinfo.iSequence = [LogicControl sharedLogicControl].CurSequence;
            
            int ilength = [logicsb ReadIntWithTT:NTT_BIG2SMALL];
            cinfo.strtext = [[FileManager shareControl] saveChatData:[logicsb ReadDataWithLength:ilength]
                                                            withIndex:[LogicControl sharedLogicControl].CurSequence];
                    
                    NSData *SpeexData = [NSData dataWithContentsOfFile:cinfo.strtext];
                    NSData *NewPCMData = DecodeSpeexToWAVE(SpeexData);
                    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:NewPCMData error:nil];
                    if (player != nil)
                    {
                        cinfo.audioPlayLength = player.duration;
                        player = nil;
                    }
                    
            cinfo.strid = [logicsb ReadUnencodeString];
            cinfo.strChatDate = [logicsb ReadUnencodeString];
            cinfo.ichatReadState = CRS_UNREAD;
            
            [chatshowarr addObject:cinfo];
            
            [[DataControl shareControl].chatDS addChatInfo:cinfo withID:cinfo.strid];
            
            [[DataBaseCtrol shareControl].chatuserdatabase insertChatInfo:cinfo];
        }
        
        mexits.strtime = cinfo.strChatDate;
        
        if ([DataControl shareControl].iGameState == GMS_CHAT &&
            [[DataControl shareControl].currentFI.mail isEqualToString:mail])
        {
            [[DataControl shareControl].UICommendDS addUICommand:UC_UPDATACHAT];
        }
        else
        {
            if (mexits.mbAlreadayArgee == true)
            {
                mexits.mbAlreadayArgee = false;
            }
            
            mexits.badgeNum++;
            
            [[DataControl shareControl].UICommendDS addUICommand:UC_RECIVECHECK];
        }
        
        [[DataBaseCtrol shareControl].messagedatabase updateMessage:mexits];
    }
    
    
    
    if (cinfo != nil) {
        [[netManager sharedNetManager].sendMsgReceipt SendMegReceipt:cinfo.strid];
    }
    
    
    
    [[AudioPlayManager shareControl] playSystem:1002];
}

@end
